/*******************************************************************************
 *  Copyright (c) 2010 Chase Technology Ltd.
 *  
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the GNU General Public License v3.0
 *  which is available at http://www.gnu.org/licenses/
 * 
 * Contributors:
 *     Doug Satchwell - initial API and implementation
 *******************************************************************************/
package org.jfunction.ui.internal.editor;

import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.IFormPage;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.texteditor.ChainedPreferenceStore;
import org.eclipse.ui.texteditor.DefaultRangeIndicator;
import org.jfunction.ui.internal.JFunctionUIPlugin;

public class SourcePage extends TextEditor implements IFormPage {

	private JFunctionEditor fEditor;
	private String id;
	private int fIndex;
	private Control fControl;

	public SourcePage(JFunctionEditor editor, String id, String title) {
		super();
		this.id = id;
		setPartName(title);
		initialize(editor);
		IPreferenceStore[] stores = new IPreferenceStore[2];
		stores[0] = JFunctionUIPlugin.getDefault().getPreferenceStore();
		stores[1] = EditorsUI.getPreferenceStore();
		setPreferenceStore(new ChainedPreferenceStore(stores));
		setRangeIndicator(new DefaultRangeIndicator());
		// setKeyBindingScopes(new String[] { "org.eclipse.ui.textEditorScope" });  //$NON-NLS-1$
		setSourceViewerConfiguration(new JFunctionEditorSourceViewerConfiguration(getSharedColors(), getPreferenceStore(), editor));
		setDocumentProvider(new JFunctionDocumentProvider());
//		if (isSelectionListener())
//			getEditor().getSite().getSelectionProvider().addSelectionChangedListener(this);
	}
	
	@Override
	public IEditorInput getEditorInput() {
		return fEditor.getEditorInput();
	}
	
	@Override
	public void createPartControl(Composite parent) {
		super.createPartControl(parent);
		Control[] children = parent.getChildren();
		fControl = children[children.length - 1];

		// PlatformUI.getWorkbench().getHelpSystem().setHelp(fControl, IHelpContextIds.MANIFEST_SOURCE_PAGE);
	}
	
	@Override
	protected void firePropertyChange(int type) {
		if (type == PROP_DIRTY) {
			fEditor.editorDirtyStateChanged();
		}
		super.firePropertyChange(type);
	}
	
	@Override
	public int getOrientation() {
		return SWT.LEFT_TO_RIGHT;
	}
	
	@Override
	protected void initializeKeyBindingScopes() {
		super.initializeKeyBindingScopes();
	//	setKeyBindingScopes(new String[] {"org.eclipse.pde.ui.pdeEditorContext"}); //$NON-NLS-1$
	}


	@Override
	public void initialize(FormEditor editor) {
		fEditor = (JFunctionEditor) editor;
		setSite(editor.getSite());
	}

	@Override
	public boolean canLeaveThePage() {
		return true;
	}

	@Override
	public FormEditor getEditor() {
		return fEditor;
	}

	@Override
	public String getId() {
		return id;
	}

	@Override
	public int getIndex() {
		return fIndex;
	}

	@Override
	public IManagedForm getManagedForm() {
		return null;
	}

	@Override
	public Control getPartControl() {
		return fControl;
	}

	@Override
	public boolean isActive() {
		return this.equals(fEditor.getActivePageInstance());
	}

	@Override
	public boolean isEditor() {
		return true;
	}

	@Override
	public boolean selectReveal(Object object) {
		if (object instanceof IMarker) {
			IDE.gotoMarker(this, (IMarker) object);
			return true;
		}
		return false;
	}

	@Override
	public void setActive(boolean arg0) {
		IEditorInput input = getEditorInput();
		setInput(input);
	}

	@Override
	public void setIndex(int arg0) {
		this.fIndex = arg0;
	}
}
